import auth from '@/utils/auth'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout/Layout'
import Login from '@/views/login/index'
import Home from '@/home/index'
import Article from '@/views/article'
import Publish from '@/views/publish'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/',
        component: Home
      },
      {
        path: '/article',
        component: Article
      },
      {
        path: '/publish',
        component: Publish
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
export default router

// 路由守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 判断是否访问登录，有无token,跳转到登录界面
  if (to.path !== '/login' && !token) return next('/login')
  next()
})
